package cn.yto.print.utils;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PrinterUtil {

    /**
     * 获取所有打印机列表
     */
    public static List<Map<String, String>> getPrinterList() {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        List<Map<String, String>> printers = new ArrayList<>();
        for (PrintService printService : printServices) {
            Map<String, String> printer = new HashMap<>();
            printer.put("name", printService.getName());
            printers.add(printer);
        }
        return printers;
    }

    /**
     * 获取当前系统的默认打印机名称
     */
    public static String getDefaultPrinter() {
        PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
        if (null != defaultPrinter) {
            return defaultPrinter.getName();
        } else {
            return null;
        }
    }

}
